﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    /// <summary>
    /// 关账
    /// </summary>
    class SubjectShareActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var model = request.Model;
            var proid = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g => this.DialogValue(request.Url.ToString())));
            if (proid.HasValue == false)
            {
                this.Prompt("参数不正确");

            }

            var sub = DataFactory.Instance().Subject(proid.Value);
            if (sub == null)
            {
                this.Prompt("参数不正确");

            }
            var Type = this.AsyncDialog("Type", g =>
             {
                 return this.DialogValue("auto");
             });

            var uri = request.Url;
            var hash = new UMC.Web.WebMeta();

            hash["src"] = UMC.Web.WebResource.Instance().ImageResolve(request.Url, sub.Id.Value, "1", "5");
            hash["title"] = sub.Title;
            
            var wiki = UMC.Web.WebResource.Instance().Provider["wiki"];
            if (String.IsNullOrEmpty(wiki))
            {
                hash["url"] = new Uri(request.Url, String.Format("/UMC/{0}/UIData/{1}/", model, sub.Id)).AbsoluteUri;
            }
            else
            {
                var item = UMC.Wiki.DataFactory.Instance().ProjectItem(sub.project_item_id.Value);
                var project = UMC.Wiki.DataFactory.Instance().Project(sub.project_id.Value);
                hash["url"] = $"{wiki}/{project?.Code}/{item?.Code}/{sub.Code}";
            }

            if (String.IsNullOrEmpty(sub.Description))
            {
                hash["title"] = sub.Title;
                hash["desc"] = sub.Description ?? sub.Title;
            }
            else
            {
                hash["desc"] = sub.Description;
            }
            hash["type"] = "Share";
            switch (Type)
            {
                case "0":
                    hash["wx"] = "0";
                    break;
                case "1":
                    hash["wx"] = "1";
                    break;
                case "2":
                    hash["wx"] = "2";
                    break;
            }
            this.Context.Send(hash, true);

        }

    }
}